package com.firstpro.service;

import com.firstpro.dao.UserDao;
import com.firstpro.domain.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhl on 2017/6/27.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 判断用户名是否存在，不存在则允许注册
     * @param user
     * @return
     */

    public int creatUser(UserEntity user) {
        String userName = user.getUserName();
        int exsists = userDao.getUserByUserName(userName);
        if (exsists > 0){
            userDao.save(user);
            return 1;
        }
        return 0;
    }

    public UserEntity getUserByUserName(String userName) {
        String hql = "from UserEntity U where U.userName = ?";
        List<UserEntity> users = (List<UserEntity>) userDao.find(hql,userName);
        if (users.size()>0){
            return users.get(0);
        } else
            return null;
    }
}
